$(function () {

    let layer = layui.layer;
    let form = layui.form;
    let laypage = layui.laypage;
    let q = {
        pagenum: 1,//页码值,默认请求第一页的数据
        pagesize: 2,//每页显示几条数据，默认每页2条
        cate_id: '',//文章分类的id
        state: ''//文章的发布状态
    }

    initTable();
    initCate();


    template.defaults.imports.dateformat = function (value) {
        let dt = new Date(value);
        let y = dt.getFullYear();
        let m = padZero(dt.getMonth() + 1);
        let d = padZero(dt.getDate());
        let hh = padZero(dt.getHours());
        let mm = padZero(dt.getMinutes());
        let ss = padZero(dt.getSeconds());

        return y + '-' + m + '-' + d + " " + hh + ':' + mm + ':' + ss;

        // return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    }

    function padZero(n) {
        return n > 9 ? n : '0' + n;
    }

    function initTable() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                if (res.status !== 0) return layer.msg('获取文章列表失败');
                // layer.msg('获取文章列表成功');
                let htmlStr = template('tpl-table', res);
                $('tbody').html(htmlStr);
                renderPage(res.total);
            }
        })
    }

    function initCate() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: function (res) {
                if (res.status !== 0) return layer.msg('获取文章分类失败');
                let htmlStr = template('tpl-cate', res);
                $('select[name=cate_id]').html(htmlStr);
                //通知layui刷新
                form.render();
            }
        })
    }

    $('#form-search').on('submit', function (e) {
        e.preventDefault();
        let cate_id = $('select[name=cate_id]').val();
        let state = $('select[name=state]').val();
        q.cate_id = cate_id;
        q.state = state;
        initTable();
    })


    function renderPage(total) {
        laypage.render({
            elem: 'box-page' //注意，这里的 test1 是 ID，不用加 # 号
            , count: total, //数据总数，从服务端得到
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function (obj, first) {
                q.pagenum = obj.curr;
                q.pagesize = obj.limit;
                if (!first) {
                    initTable();
                }
            }
        });
    }


    $('tbody').on('click', '.btn-delete', function () {
        let len = $('.btn-delete').length;
        let Id = $(this).attr('data-id');
        layer.confirm('确定删除吗?', { icon: 3, title: '提示' }, function (index) {
            //do something


            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + Id,
                success: function (res) {
                    if (res.status !== 0) return layer.msg('删除文章失败');
                    layer.msg('删除文章成功');


                    if (len === 1) {
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1;
                    }


                    layer.close(index);
                    initTable();
                }
            })

        });

    })


})